package com.kr.start;

import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; 
import springfox.documentation.builders.ParameterBuilder; 
import springfox.documentation.schema.ModelRef; 
import springfox.documentation.service.Parameter;

/**
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/4/30 14:19:31
 */
@Configuration
@EnableSwagger2
public class Swagger2Init {
    @Bean
    public Docket createRestApi() {
    	ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("token")                 
                         .modelRef(new ModelRef("string"))
                         .parameterType("header")               
                         .defaultValue("")        
                         .required(false)                
                         .build();
        java.util.List<Parameter> aParameters = new ArrayList<>();
        aParameters.add(aParameterBuilder.build());
        
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(basePackage("com.kr.start"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("")
                .globalOperationParameters(aParameters);
    }

    @SuppressWarnings("deprecation")
	private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试模块")
                .description("测试模块")
                .termsOfServiceUrl("zc8023zs@163.com")
                .version("1.0")
                .build();
    }
    
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            for (String strPackage : basePackage.split(";")) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    @SuppressWarnings("deprecation")
	private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}
